#!/usr/bin/env python
from lib.format_time import *
from lib.HandleMysql import *
from lib.switch_code import *
from shop_conn import *
import sys
import re
reload(sys)
sys.setdefaultencoding('utf-8')

def get_order_data(date):
    
    print "set_mid_data bigin : %s"%date
    
    conn = set_order_conn()
    stat_conn = set_stats_conn()
    prod_conn = set_product_conn()
    shop_conn = set_shop_conn()
    
    stat_table = "order_action"
    
    #get data
    query = "select orderid,ordertotal from t_order " 

    items = conn.get_results(query)
    for item in items:
        
        order_id = item[0]            
        totalfee = item[1]
        
        update_sql = "update order_action set pay_amount=%s where order_id=%s"%(totalfee,order_id)
        ##print update_sql
        stat_conn.execute_sql(update_sql)
    pass

#logic
def execute(date):
    # get main data
    # from order orderproduct orderticket orderpay orderlog
    get_order_data(date)
    pass
def main():

   #date = '2012-05-28'
   #execute(date)   
   if len(sys.argv)==1:
        start_date=get_yesterday()
        end_date=start_date
   elif len(sys.argv)==2:
        start_date=format_date(sys.argv[1])
        end_date=start_date
   elif len(sys.argv)==3:
        start_date=format_date(sys.argv[1])
        end_date=format_date(sys.argv[2])
   stat_date=start_date
   while stat_date>=start_date and stat_date<=end_date:
        print "The program of the date of %s is beginning at %s " %(stat_date,get_now())
        #
        execute( stat_date) 
        print "The program of the date of %s is end at %s " %(stat_date,get_now())
        stat_date=var_date(stat_date)


if __name__=='__main__':
    main()

